package com.soap.desensitize.strategy.irreversible;


import com.soap.desensitize.encry.irreversible.AbstractIRreversibleEncry;
import java.security.NoSuchAlgorithmException;

/**
 * @author HJH
 * @desc Hmac对称加密
 * @time 2022-04-21
 */
public class HmacStrategy extends AbstractIRreversibleEncry<String> {
    private static HmacStrategy instance = null;
    private HmacStrategy(){}

    /**
     * @desc 获得实例
     * @return
     */
    public static synchronized HmacStrategy getInstance(){
        if(instance == null){
            instance = new HmacStrategy();
        }
        return instance;
    }

    /**
     *
     * @param data 加密字符串
     * @return
     * @throws NoSuchAlgorithmException
     */
    @Override
    public String encode(String data) {
        StringBuffer stringBuffer = new StringBuffer();//定义一个字符串缓冲区
        //定义一个字符串
        String temp = null;
        for (int i = 0; i < data.getBytes().length; i++) {
            // 将字节数据转换为十六进制
            temp = Integer.toHexString(data.getBytes()[i] & 0xFF);
            if (temp.length() == 1) {
                //得到一位的进行补0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

}